package com.example.wyemail;



import cn.hutool.core.util.PhoneUtil;
import com.example.wyemail.util.DataUtil;
import com.example.wyemail.util.IconUtil;
import com.example.wyemail.util.SoftInitUtil;
import org.apache.commons.io.file.PathUtils;
import org.sikuli.script.*;

import java.awt.*;
import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
import java.util.List;

import static com.example.wyemail.util.EmailAccountUtil.addEmailAccount;

/**
 * 电子邮件测试
 *
 * @author fxab
 * @date 2024/06/07
 */
public class EmailTest {
    /**
     * 测试主程序
     *
     * @param args args
     * @throws IOException            ioexception
     * @throws FindFailed             查找失败
     * @throws InterruptedException   中断异常
     * @throws IconUtil.ImageNotFoundException 未找到图像异常
     */
    public static void main(String[] args) throws IOException, FindFailed, InterruptedException, IconUtil.ImageNotFoundException {
        //所需的路径：软件路径、邮件保存路径，后续还需要账号密码文档的路径
        String softDirectory = "C:\\Program Files (x86)\\Netease\\MailMaster\\Application\\mailmaster.exe";
        String mainDirectory = "D:\\MailMasterData";
        //打开客户端
        SoftInitUtil.Start(softDirectory);
        Thread.sleep(5000);
        //打开添加邮箱页面
        File directory = new File("");
        String courseFile = directory.getCanonicalPath() ;
        String imgPath = courseFile + "\\src\\main\\resources\\images\\";
        Screen s = new Screen();
        String icon1Img = imgPath + "icon1.png";
        String emailSetImg = imgPath + "emailSet.png";
        Thread.sleep(3000);
        // 防止上一次关闭客户端时保留了这个“绑定手机”的弹窗存在
        String noPhoneImg = imgPath + "noPhone.png";
        if (s.exists(noPhoneImg) != null) {  // 等待 1 秒查找图像
            Pattern noPhone = new Pattern(noPhoneImg);
            Match match = s.find(noPhone);
            Location topLeft = match.getTopLeft();
            int topLeftX = topLeft.getX();
            int topLeftY = topLeft.getY();
            int targetX = topLeftX + 200;
            int targetY = topLeftY - 45;
            s.click(new Location(targetX, targetY));
        }
        if(IconUtil.IsExist(s,icon1Img)){
            Pattern icon1 = new Pattern(imgPath + "icon1.png");
            Match match = s.find(icon1);
            Location topLeft = match.getTopLeft();
            int topLeftX = topLeft.getX();
            int topLeftY = topLeft.getY();
            int targetX = topLeftX + 28;
            int targetY = topLeftY + 276;
            s.click(new Location(targetX, targetY));
        }
        if(IconUtil.IsExist(s,emailSetImg)){
            s.click(emailSetImg);
        }
        Thread.sleep(1000);
        List<String[]> emailAccounts = new ArrayList<>();

        String accountDocument = "./src/main/resources/accounts.txt";
        // 读取文件并解析内容
        try (BufferedReader br = new BufferedReader(new FileReader(accountDocument))) {
            String line;
            // 从第1行开始读取
            while ((line = br.readLine()) != null) {
                String[] parts = line.split("\\s+");
                if (parts.length >= 2) {
                    String email = parts[0];
                    String password = parts[1];
                    String phone = parts[2];
                    emailAccounts.add(new String[]{email, password, phone});
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        // 循环添加每个邮箱账号
        Integer emailSum = 0;
        for (String[] account : emailAccounts) {
            String email = account[0];
            String password = account[1];
            addEmailAccount(email, password);
            emailSum++;
        }

        // 关闭客户端
        Thread.sleep(6000);
        SoftInitUtil.Stop();

        // 提取邮箱数据
        DataUtil.getData(mainDirectory,accountDocument,courseFile+"/");
        // 删除邮箱数据
        DataUtil.deleteData(mainDirectory,accountDocument);
        System.out.println("终止程序");
        System.exit(0);

    }
}
